<?php

namespace App\Admin\Controllers;

use App\Http\Models\Goods;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Models\User;
use App\Http\Models\UserSubmitAudit;
use App\Http\Models\UserCapital;
use App\Http\Models\StatisticsUser;

class UserPermissionController extends Controller
{
    use ModelForm;
    protected $headerStr = '放单权限';
    protected $status = 0;
    protected $is_unaudited;


    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        $this->is_unaudited = request('is_unaudited');
        return Admin::content(function (Content $content) {
            $content->header($this->headerStr . '管理');
            $content->body($this->grid());
        });
    }

    public function edit($id)
    {
        $re = UserSubmitAudit::where(['id' => $id])->first();
        $this->status = $re->state;
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form()->edit($id));
        });
    }


    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form());
        });
    }


    protected function grid()
    {
        return Admin::grid(UserSubmitAudit::class, function (Grid $grid) {
            if ($this->is_unaudited) {
                $grid->model()->where(['state' => 0])->orderBy('state', 'asc')->orderBy('id', 'desc');
            } else {
                $grid->model()->orderBy('state', 'asc')->orderBy('id', 'desc');
            }
            $grid->id('编号')->sortable();
            $grid->uid('用户ID(昵称)')->display(function () {
                $userinfo = User::find($this->uid);
                return $this->uid . '(' . $userinfo->name . ')';
            })->sortable();
            $grid->user()->avatar('头像')->display(function ($avatar) {
                return user_image_url($avatar);
            })->image(40, 40);
            $grid->phone('手机号');
            $grid->state('状态')->display(function ($value) {
                return $value == 1 ? '<span class=" label label-success">审核通过</span>' : ($value == 2 ? '<span class=" label label-warning">审核被拒</span>' : '<span class=" label label-default">审核中</span>');
            });
            $grid->created_at('提交时间');
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();//禁用查询过滤器
                $filter->equal('state', '审核状态')->select(['0' => '未审核', '1' => '审核通过', '2' => '审核被拒']);
                $filter->equal('uid', '用户ID');
                $filter->equal('phone', '用户手机号');
            });
            if (Admin::user()->isRole('administrator') || Admin::user()->isRole('operator')) {
                $grid->actions(function ($actions) {
                    $actions->disableDelete();
                    if(method_exists($actions,'disableView')){
                        $actions->disableView();
                    }
                });
            } else {
                $grid->disableActions();
            }
            $grid->disableCreateButton();
            $grid->perPages([10, 20, 30, 40, 50]);
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->paginate(env('Admin_Limit'));
            //$grid->disableActions();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(UserSubmitAudit::class, function (Form $form) {
            $form->display('id', 'ID:');
            $form->hidden('uid');
            $form->text('industry', '行业名称:');
            $form->text('linker', '联系人:');
            $form->text('qq', 'QQ:');
            $form->text('phone', '手机号码:');
            $form->textarea('content', '详细介绍:');
            // if ($this->status != 1) {
            $form->select('state', '审核状态')->options(['1' => '审核通过', '2' => '审核被拒'])->rules('required', [
                'required' => '审核状态必填',
            ]);
            $form->text('refuse', '被拒原因:');
            //}
            $form->disableReset();
            $form->saving(function (Form $form) {
                $re = UserSubmitAudit::where(['id' => $form->model()->id])->first();
                if (request('state') == 1 && $re->state == 0) {
                    $content = '放单权限通过奖励' . env('Voucher_Default_Give') . '点券';
                    UserCapital::addUserCapital($form->model()->uid, env('Voucher_Default_Give'), 2, $content);
                    StatisticsUser::addOrderPermissionsUser(); //增加放单权限用户数量
                    UserCapital::addUserIntegral($form->model()->uid, env('UserIntegral'), 2, '用户放单权限通过,初始化积分为20', 0, '');
                } elseif (request('state') == 2 && $re->state == 0) {
                    StatisticsUser::decOrderApplyUser(); //降低下单申请用户
                }
            });
        });
    }
}